<?php
    defined('_JEXEC') or die(); 
    JHTML::_('behavior.tooltip'); 
    $listOrder    = @$this->lists['order'];
    $listDirn    = @$this->lists['order_Dir'];
	
?>
<style>
    #tbl_filter{
        margin: 0 ; padding: 0 ;
    }
    #tbl_filter td{
        padding: 3px;
    }
    #tbl_filter input, #tbl_filter img {
        float: left;
        margin: 0 5px 0 0;
        width: auto;
    }
    table.adminlist tbody tr td { padding: 8px 8px; }
    table.adminlist thead th { padding: 8px; }
</style>
<link rel="stylesheet" type="text/css" href="<?php echo JURI::root(); ?>js/multiselect/jquery.multiselect.css" />
<link rel="stylesheet" type="text/css" href="<?php echo JURI::root(); ?>js/multiselect/jquery-ui.css" />
<script type="text/javascript" src="<?php echo JURI::root(); ?>js/jquery.js"></script>
<script type="text/javascript" src="<?php echo JURI::root(); ?>js/jquery-ui.min.js"></script>
<script type="text/javascript" src="<?php echo JURI::root(); ?>js/multiselect/jquery.multiselect.js"></script>

<script language="javascript" type="text/javascript">
    function submitform(pressbutton){
        var form = document.adminForm ;
        //alert(pressbutton)
        if (pressbutton)
        {
            form.task.value=pressbutton ;
        }
        if ((pressbutton=='send_redemp_notice_email')||(pressbutton=='send_redemp_pick_up_notice')){
              form.controller.value="reports" ;
        }
        form.submit();
    }
	jQuery(function(){
		jQuery("#topDisplaySelect").change(function(){
			jQuery("#limit").val(jQuery(this).val());
			submitform();
		});
		jQuery("#topDisplaySelect").val(jQuery("#limit").val());
	});
    jQuery(function(){ 
        jQuery("#statisitc_by").val("<?php echo $this->lists['statistic_by'] ?>");
        jQuery("#pick_up_at").multiselect({
            selectedList: 0,
            noneSelectedText: '-- All --',
            selectedText: "# of # selected"
        }); 
    });
    
</script>
<div class="col width-100">    
    
        <div id="editcell">
            <form action="<?php echo JRoute::_("index.php?option=com_wrd&controller=reports")?>" method="post" name="filterform" >
                <table style='margin-bottom:5px' id="tbl_filter">
                    <tbody>
                        <tr>
                            <td align="left" valign="top" style="border-right: 1px solid #d0d0d0;">
                                <table >
                                    <tr>
                                        <td style="text-align: left;padding: 10px 3px;">
                                             From:
                                        </td>
                                        <td>
                                            <?php
                                                echo JHTML::_ ( 'calendar', ($this->pr['date_from'] != '') ? $this->pr['date_from'] : date('Y-m-d', mktime(0, 0, 0, date('m'), 1, date('Y'))), 'date_from', 'date_from', '%Y-%m-%d');
                                            ?>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td style="text-align: left;padding: 10px 3px;">To:</td>
                                        <td>
                                            <?php
                                                echo JHTML::_ ( 'calendar', ($this->pr['date_to'] != '') ? $this->pr['date_to'] : date('Y-m-d', time()), 'date_to', 'date_to', '%Y-%m-%d');
                                            ?>
                                        </td>
                                    </tr>
                                </table>                                
                            </td>                             
                             <td align="left" valign="top" id="statistic_by_job" style="border-right: 1px solid #d0d0d0;">
                                <table>                                  
                                    <tr>
                                        <td style="text-align: left;padding: 10px 3px;">
                                             Status:
                                        </td>
                                        <td>
                                            <?php echo $this->lists['extra_status']; ?>
                                        </td>
                                    </tr>                                                                                                                                     
                                   
                                </table>
                            </td>
                            
                            <td align="left" valign="top" id="last_filter_column">
                                <table >
                                    <tr>
                                       
                                        <td>
                                            Choose: 
											
											<select name="topDisplaySelect" id="topDisplaySelect">
                                                <option value="10">5</option>
												<option value="10">10</option>
                                                <option value="10">15</option>
												<option value="20">20</option>
                                                <option value="50">30</option>
												<option value="50">50</option>
                                                <option value="100">100</option>
                                                <option value="0">All</option>
                                            </select>
                                        </td>
                                    </tr>
									<tr valign="top">
                                        <td style="text-align: left;padding: 10px 3px 0;">
											<input type="submit" name="view" onclick="document.getElementById('submitViewExcel').value = 'submitViewExcel';  document.getElementById('task').value = 'redemptionsummary';" value="View" />	
											<input type="submit" name="summary" onclick="document.getElementById('submitViewExcel').value = 'submitViewExcel';  document.getElementById('task').value = 'redemptionsummary';" value="Summary" />
											<input type="submit" name="detail" onclick="document.getElementById('submitViewExcel').value = 'submitViewExcel';  document.getElementById('task').value = 'redemptiondetail';" value="Detail" />
										</td>
                                    </tr>
									<tr valign="top">
                                        <td colspan="2" style="text-align: left;padding: 10px 3px 0;">
                                        <input type="submit" name="exportexcel" onclick="document.getElementById('submitViewExcel').value = 'submitViewExcel';  document.getElementById('task').value = 'exredemptionsummary';" value="Export to excel" /></td>
                                    </tr>
                                </table>
                            </td>                       
                        </tr>
                    </tbody>
                </table>
                <input type="hidden" name="controller" value="reports" />
                <input type="hidden" name="option" value="com_wrd" />
                <input type="hidden" name="submitViewExcel" id="submitViewExcel" value="" />
                <input type="hidden" name="task" id="task" value="" />
            </form>    
            <br />
            <form id="adminForm" action="<?php echo JRoute::_("index.php?option=com_wrd&controller=reports")?>" method="post" name="adminForm" >
				<!--<link type="text/css" href="/js/dialog/css/sunny/jquery-ui-1.8.20.custom--.css" rel="stylesheet" />-->
<script type="text/javascript" src="<?php echo JURI::root(); ?>js/dialog/jquery-ui-1.8.20.custom.min.js"></script>
<script type="text/javascript">
    jQuery(function(){

        // Dialog
        jQuery('#dialog').dialog({
            autoOpen: false,
            width: 300,
            height: 200,
            open: function(){
                jQuery("#pick_up_date_obj").val(jQuery("#pick-up-date_"+jQuery("#pick_up_date_member").val()).find("span").html());
                jQuery(this).dialog("open");
            },
            buttons: {
                "Ok": function() {
                    that = this;
                    
                    /***** Init Query String *****/
                    var queryString = 'id='+jQuery("#pick_up_date_member").val();                    
                    queryString += "&t="+jQuery("#delivery_or_pick_up").val();
                    queryString += "&d="+jQuery("#pick_up_date_obj").val();
                    
                    jQuery.ajax({
                        type: "POST",
                        url: "/administrator/index.php?option=com_wrd&controller=reports&layout=redemptionreport&task=updateDateRedemption",
                        data: queryString,
                    }).success(function( json ) {
                        var result = eval(json);
                        if(result.success == true) {
                            if(jQuery("#delivery_or_pick_up").val() == 1) {
                                jQuery("#pick-up-date_"+jQuery("#pick_up_date_member").val()).find("span").html(result.html);    
                            } else {
                                jQuery("#deliver-to-outlet-date_"+jQuery("#pick_up_date_member").val()).find("span").html(result.html);
                            }
                            
                        }
                        jQuery(that).dialog("close");
                    });
                },
                "Cancel": function() {
                    jQuery(this).dialog("close");
                }
            }
        });
        jQuery('.pick-up-date').click(function(){
            var idMember = jQuery(this).attr("name").split("_");
            jQuery("#pick_up_date_member").val(idMember[1]);
            jQuery("#delivery_or_pick_up").val(1);
            jQuery('#dialog').dialog('open');
            return false;
        });
        
        jQuery('.deliver_to_outlet-date').click(function(){
            var idMember = jQuery(this).attr("name").split("_");
            jQuery("#pick_up_date_member").val(idMember[1]);
            jQuery("#delivery_or_pick_up").val(2);            
            jQuery('#dialog').dialog('open');
            return false;
        });

    });
</script>
<style>
.extra-adminlist strong { color:#025A8D; font-size: 1.2em; font-weight: bold; }
table.extra-adminlist .extra-adminlist-tr td {background: #F7F7F7; }
.filter-detail { border-bottom: 1px solid #CCC; padding-bottom: 3px; font-size: 1.4em; }
.filter-detail span { color: #e72222; }
</style>
<?php 
    $complainType = JHTMLwrd::listComplainTypeRaw();
    $mealPeriod = JHTMLwrd::listMealPeriodRaw();
?>
<script type="text/javascript">
jQuery(function(){
    
    //jQuery("#statistic_by").html(jQuery("#statistic_by").html() + " <span style='color:#000;'>Complain Type:</span> " + getSelectedOptionFilter("extra_type")); 
    jQuery("#statistic_by").html(jQuery("#statistic_by").html() + "<span style='color:#000;'>Products:</span> " + getSelectedOptionFilter("extra_products")); 
    jQuery("#statistic_by").html(jQuery("#statistic_by").html() + ". <span style='color:#000;'>Outlet:</span> " + getSelectedOptionFilter("pick_up_at")); 
    //jQuery("#statistic_by").html(jQuery("#statistic_by").html() + ". <span style='color:#000;'>By Location:</span> " + getSelectedOptionFilter("extra_district")); 
    
});

function getSelectedOptionFilter(listName)
{
    var html = "";
    jQuery("#"+listName+" option[selected=selected]").each(function(){
        html = html + jQuery(this).html() + ", ";
    });
    return html.substring(0, html.length-2);
}
</script>
<h3 class="filter-detail"><span id="statistic_by"></span><span style="color:#C00; float:right;">Total: <?php echo $this->pagination->total; ?></span></h3>
<table width="100%" class="adminlist extra-adminlist">
  <thead>
    <tr class="extra-adminlist-tr" >
        <th nowrap="nowrap"><strong>No</strong></th>
        <th nowrap="nowrap"><strong>Outlet</strong></th>
		<th colspan="3">
			<table width="100%">
				<tr>
					<th width="60%" style="border: none;"><strong>Item</strong></th>
					<th width="20%" style="border: none;" align="right"><strong>Quantity</strong></th>
					<th width="20%"  style="border: none;"><strong>Charge Cost</strong></th>
				</tr>
			</table>
    </tr>
  </thead>
  <tbody>
    <?php 
	 $arr = array();
     if(count($this->items)){
        foreach($this->items as $item){
            $outlet_id    = $item->outletName;
            $product_id   = $item->product_id;
			$product_cost   = $item->product_cost;
            $quantity     = $item->quantity;
            $charge_cost  = $item->charge_cost;
            $product_name = $item->productName;

            if(!isset($arr[$outlet_id])) $arr[$outlet_id] = array();                    
            if(!isset($arr[$outlet_id][$product_id])){
                $arr[$outlet_id][$product_id] = array(
                    "productName" => $product_name,
                    "quantity"    => $quantity,
                    "charge_cost" => $charge_cost,
					"product_cost" => $product_cost
                );
            }else{
                $arr[$outlet_id][$product_id]['quantity']    += $quantity;
                $arr[$outlet_id][$product_id]['charge_cost'] += $charge_cost;
            }
        }
    }
   // echo "<hr />";echo '<pre>';print_r($arr);
	   
	// echo "<hr />";echo '<pre>';print_r($arr); 
	   /******** Get Redemption Ids ********/
        
     ?>
    <?php 
 
         //  foreach($this->items as $item):
                
			  /// $product = WrdHelper::getProductByCodeRedem($item->code);
			   //$total = WrdHelper::totalPointByCodeRedem($item->code);
			$i=0;
			foreach($arr as $k => $v):
				echo '<tr class="row'.($bg).'" valign="top">';
				echo '<td  style="">'.$i.'</td>';
				echo '<td  style="">'.$k .'</td>';
				
				echo '<td><table width="100%">';
					
					foreach($v as $key => $value):
						echo '<tr>';
							echo '<td width="60%">'.$value['productName'] .'</td>';
							echo '<td align="right" width="20%">'.$value['quantity'] . '</td>';
							echo '<td align="right" width="20%">'.number_format($value['product_cost'] * $value['quantity'] * $value['quantity'],0,',',',').'</td>';
						echo '</tr>';
					endforeach;
				echo '</table></td>'; 
				 echo '</tr>';
			  ++$i;
			endforeach;
    ?>
    
  </tbody>
  <tfoot>
        <tr>
            <td colspan="16">
                <?php
                     echo $this->pagination->getListFooter();
                ?>
            </td>
        </tr>
    </tfoot>
</table>
<!-- ui-dialog -->
<div id="dialog" title="Update Gift Redemption">
    <div class="dialog-content">
        <div class="dialog-pick-up-date">
            <?php
                echo JHTML::_ ( 'calendar', date('Y-m-d', mktime(0, 0, 0, date('m'), 1, date('Y'))), 'pick_up_date_obj', 'pick_up_date_obj', '%Y-%m-%d');
            ?>
            <input type="hidden" id="pick_up_date_member" name="pick_up_date_member" value="" />
            <input type="hidden" id="delivery_or_pick_up" name="delivery_or_pick_up" value="" />
        </div>
    </div>
</div>
                <input type="hidden" name="controller" value="reports" />
                <input type="hidden" name="option" value="com_wrd" />
                <input type="hidden" name="task" value="redemptionreportnew" />
                <input type="hidden" name="boxchecked" value="0" />
                <?php echo JHTML::_( 'form.token' ); ?>
            </form>
        </div>
        
</div>